/* Team 5
 * James McQueen
 * Corwyn Simpson
 * May 1 ish, 2012
 */
package spider.models;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * An wrapper of the URL class that has non-blocking equals() and hashCode(),
 * and normalizes the host to reduce false negatives without using DNS name 
 * resolution.
 * 
 * @author Corwyn Simpson
 */
public class FastURL
{
    private static final int HASH_MULT = 29;
    
    private URL my_url;
    
    public FastURL(URL the_context, String the_spec) throws MalformedURLException
    {
        my_url = new URL(the_context, the_spec);
    }
    
    public FastURL(URL the_url)
    {
        my_url = the_url;
    }
    
    public int hashCode()
    {
        int result = 0;
        result = result * HASH_MULT + my_url.getProtocol().hashCode();
        result = result * HASH_MULT + my_url.getAuthority().hashCode();
        result = result * HASH_MULT + my_url.getPath().hashCode();
        return result;
    }
    
    public boolean equals(Object the_other)
    {
        boolean result = false;
        if (the_other != null && the_other instanceof FastURL)
        {
            FastURL other_url = (FastURL) the_other;
            result = my_url.getProtocol().equals(other_url.my_url.getProtocol() )
                   && my_url.getAuthority().equals(other_url.my_url.getAuthority())
                   && my_url.getPath().equals(other_url.my_url.getPath());
        }
        return result;
    }
     
    public URL getURL()
    {
        return my_url;
    }
}
